<template>
  <div class="container">
    <el-card class="box-card" shadow="always">
      <el-row :gutter="24">
        <el-col :span="16">
          <el-form inline @submit.native.prevent="search">
            <el-form-item label="搜索">
              <el-input
                v-model="searchForm.queryConditions"
                clearable
                placeholder=""
                prefix-icon="el-icon-search"
                size="medium"
              />
            </el-form-item>
            <el-form-item label="标案类型">
              <el-select
                v-model="productTypeList.productType"
                clearable
                placeholder="全部"
                @change="productTypeSelected"
              >
                <el-option
                  v-for="item in productTypeList"
                  :key="item.id"
                  :value="item.id"
                  :label="item.productType"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button native-type="submit" size="medium" type="primary"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="8">
          <el-button
            icon="el-icon-s-promotion"
            type="warning"
            class="btn"
            @click="commitAllTenderApply"
          >
            竞标申请
          </el-button>
        </el-col>
      </el-row>
      <div>
        <el-table
          :data="list"
          border
          style="width: 100%"
          @selection-change="handleSelectChange"
          v-loading="bidApplyLoading"
        >
          <el-table-column type="selection" width="55" align="center" />

          <el-table-column
            type="index"
            width="50"
            label="序号"
            align="center"
          />

          <el-table-column prop="tenderCode" label="标案编码" align="center" />
          <el-table-column prop="tenderName" label="标案名称" align="center" />
          <el-table-column label="招标方式" align="center">
            <template slot-scope="scope">
              <el-tag type="warning">{{ scope.row.tenderMode === 0 ? '公开招标':'非公开招标'}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="purchaser" label="管理人" align="center" />
          <el-table-column prop="mobilePhone" label="联系电话" align="center" />
          <el-table-column prop="startTime" label="开始时间" align="center" />
          <el-table-column label="当前状态" align="center">
            <el-tag type="warning">未开始</el-tag>
          </el-table-column>

          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-tooltip content="查看标案详细信息" placement="top">
                <el-button
                  size="mini"
                  icon="el-icon-search"
                  circle
                  @click="showDialog(scope.row)"
                />
              </el-tooltip>
              <el-tooltip content="竞标申请" placement="top">
                <el-button
                  size="mini"
                  type="warning"
                  icon="el-icon-s-promotion"
                  circle
                  @click="commitTenderApply(scope.row)"
                />
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>

        <el-pagination
          :current-page="paging.current"
          :page-sizes="paging.pageSizes"
          :page-size="paging.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="paging.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>

      <Dialog
        :visible="tenderInfoVisible"
        :data="tenderInfo"
        @cancel="cancel"
      />
    </el-card>
  </div>
</template>

<script>
import {
  getAllTenderCanBeApproved,
  commitTenderApplication,
} from "@/api/supplierTender.js";
import { getTenderAllInfoByTenderId } from "@/api/purchaser-tender.js";
import { getProductTypeBriefInfos } from "@/api/productType";
import Dialog from "../../purchaser-tender/noApprove/components/Dialog";

export default {
  name: "ManagerTenderVerify",
  components: { Dialog },
  data() {
    return {
      searchForm: {
        queryConditions: "",
        typeId: "",
      },
      productTypeList: [],
      list: [],
      paging: {
        total: 0,
        current: 1,
        pageSizes: [5, 10, 15],
        size: 5,
      },
      tenderInfoVisible: false,
      bidApplyLoading: false,
      tenderInfo: {},
      dialogTitle: "",
      multipleSelection: [], //复选框选中的所有数据
      index: 0, // 0 添加；1 编辑；2 查看信息
    };
  },

  created() {
    this.bidApplyLoading = true;
    this.getData();
    this.getProductType();
    // console.log(getToken())
  },
  methods: {
    async getProductType() {
      const result = await getProductTypeBriefInfos();
      this.productTypeList = result.data;
      // console.log('@@@@',this.productTypeList)
    },
    search() {
      this.paging.current = 1;
      this.bidApplyLoading = true;
      this.getData();
    },
    getData() {
      const form = {
        ...this.searchForm,
        page: this.paging.current,
        pageSize: this.paging.size,
        supplierId: this.$store.getters.accountId,
      };

      getAllTenderCanBeApproved(form)
        .then(({ data }) => {
          console.log(data);
          this.list = data.supplierTenderInfos;
          //this.paging.size = data.size
          this.paging.total = data.totalCount;
          this.bidApplyLoading = false;
        })
        .catch((err) => {
          // 如果没有数据就显示为空
          this.list = [];
          // 返还每页显示的条数
          //this.paging.size = res.data.size;
          this.paging.total = 0;
          this.bidApplyLoading = false;
        });
    },

    // 获取到选中的所有数据
    handleSelectChange(val) {
      console.log(val);
      this.multipleSelection = [];
      val.forEach((value) => {
        // 如果复选框选中的是待提交标案
        this.multipleSelection.push(value.id);
      });

      console.log(this.multipleSelection);
    },
    handleSizeChange(val) {
      this.paging.current = 1;
      this.paging.size = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.paging.current = val;
      this.getData();
    },
    showDialog(row) {
      this.index = 2;
      this.getTenderInfoById(row.id);
    },
    getTenderInfoById(tenderId) {
      getTenderAllInfoByTenderId({ tenderId: tenderId })
        .then((res) => {
          if (res.code === 200) {
            this.tenderInfo = res.data;
            if (this.index === 2) {
              this.tenderInfoVisible = true;
            } 
          }
        })
        .catch((err) => {
          this.$message("warning", "正在加载中！");
        });
    },
    productTypeSelected(val) {
      this.searchForm.typeId = val;
      this.search();
    },

    commitTenderApply(row) {
      // 审核资质
      this.$confirm("提交标案竞标申请, 是否继续?", "提示", {
        distinguishCancelAndClose: true,
        confirmButtonText: "继续",
        cancelButtonText: "放弃",
        type: "warning",
      })
        .then(async () => {
          const verifyData = {
            supplierId: this.$store.getters.accountId,
            tenderIds: [],
          };
          verifyData.tenderIds.push(row.id);
          this.approve(verifyData, "竞标申请已成功提交！");
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消当前操作",
          });
        });
    },
    commitAllTenderApply() {
      if (this.multipleSelection.length == 0) return;
      // 提交标案申请
      this.$confirm("提交标案竞标申请, 是否继续?", "提示", {
        distinguishCancelAndClose: true,
        confirmButtonText: "继续",
        cancelButtonText: "放弃",
        type: "warning",
      })
        .then(async () => {
          const verifyData = {
            supplierId: this.$store.getters.accountId,
            tenderIds: [],
          };
          verifyData.tenderIds = this.multipleSelection;
          this.approve(verifyData, "竞标申请已全部提交");
        })
        .catch((action) => {
          this.$message({
            type: "info",
            message: "取消当前操作",
          });
        });
    },
    approve(verifyData, message) {
      commitTenderApplication(verifyData)
        .then((result) => {
          console.log(result);
          if (result.code === 200) {
            this.$message({
              type: "success",
              message: message,
            });
            this.getData();
          }
        })
        .catch((err) => {
          this.$message({
            type: "error",
            message: err,
          });
        });
    },
    // 对象的深拷贝
    // 对象进行深拷贝，否则“编辑”框内修改内容也会影响到页面
    extendCopy(p) {
      var c = {};
      for (var i in p) {
        c[i] = p[i];
      }
      return c;
    },
    
    cancel() {
      if (this.index === 2) {
        this.tenderInfoVisible = false;
      } else {
        this.editDialogVisible = false;
      }
      this.index = 0;
    },
  },
};
</script>
<style lang="scss" scoped>
.el-pagination {
  margin-top: 20px;
  text-align: right;
}
.btn {
  float: right;
  margin-left: 20px;
}
</style>
